package com.example.yoho.mvp.view.adapter;

import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.yoho.R;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.model.entity.CommunityBean;
import com.example.yoho.utils.MyGlide;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CommunityAdapters extends BaseQuickAdapter<CommunityBean.ValuesBean, BaseViewHolder> {

    LinearLayout layout1;

    public CommunityAdapters(int layoutResId, @Nullable List<CommunityBean.ValuesBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommunityBean.ValuesBean item) {
        ImageView imageView = helper.getView(R.id.head_img);
        ImageView mes = helper.getView(R.id.images_home_mes);
        ImageView unlike = helper.getView(R.id.images_home_unlike);

        TextView title = helper.getView(R.id.title);
        TextView name = helper.getView(R.id.name);
        TextView textss = helper.getView(R.id.textss);
        TextView time = helper.getView(R.id.time);
        TextView num_mes = helper.getView(R.id.mes);
        TextView num_unlike = helper.getView(R.id.unlike);


        layout1 = helper.getView(R.id.ll_img1);
        LinearLayout layout2 = helper.getView(R.id.ll_img2);
        LinearLayout layout3 = helper.getView(R.id.ll_img3);

        MyGlide.GlideFor(mContext,Domain_Url.IMAGE_URL + item.getImgs().get(0).getImg_path() + ".jpg", imageView);
        title.setText(item.getTitle());
        name.setText(item.getTag());
        textss.setText(item.getCommunity_values());
        num_mes.setText(item.getCollection());
        num_unlike.setText(item.getFabulous());
        Date date1 = new Date(((long) Integer.valueOf(item.getCommunity_time())) * 1000);
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
        time.setText(format.format(date1));
        layout1.removeAllViews();
        layout2.removeAllViews();
        layout3.removeAllViews();
        switch (item.getImgs().size()) {
            case 1:
                for (int i = 0; i < 3; i++) {
                    layout1.addView(getview(item));
                }
                break;
            case 2:
            case 3:
                for (int i = 0; i < item.getImgs().size(); i++) {
                    layout1.addView(getviews(item, i));
                }
                break;
            case 4:

                for (int i = 0; i < item.getImgs().size(); i++) {
                    if (i < 2)
                        layout1.addView(getviews(item, i));
                    else
                        layout2.addView(getviews(item, i));
                }
                break;
            case 5:
            case 6:
                for (int i = 0; i < item.getImgs().size(); i++) {
                    if (i < 3)
                        layout1.addView(getviews(item, i));
                    else
                        layout2.addView(getviews(item, i));
                }
                break;
            default:
                for (int i = 0; i < item.getImgs().size(); i++) {
                    if (i < 3)
                        layout1.addView(getviews(item, i));
                    else if (i < 6)
                        layout2.addView(getviews(item, i));
                    else
                        layout3.addView(getviews(item, i));
                }
                break;
        }


    }

    private View getview(CommunityBean.ValuesBean item) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.img, layout1, false);
        ImageView viewById = inflate.findViewById(R.id.mimg);
        MyGlide.GlideFor(mContext,Domain_Url.IMAGE_URL + item.getImgs().get(0).getImg_path() + ".jpg", viewById);
        return inflate;
    }

    private View getviews(CommunityBean.ValuesBean item, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.img, layout1, false);
        ImageView viewById = inflate.findViewById(R.id.mimg);
        MyGlide.GlideFor(mContext,Domain_Url.IMAGE_URL + item.getImgs().get(i).getImg_path() + ".jpg", viewById);
        return inflate;
    }

}
